<?php
//用户
namespace Common\Model;
class UserModel extends BaseModel {

	//追加属性
    public $append = [
        'avatar',
        'sex_text',
        'type_name',
        'yewu_name',     //业务员姓名
        'attr_text', //客户属性
        'amount',
        'credit',
	];

    //金额
    public function getAmountAttr($value, $data){
		return floatval($data['amount']);
    }

    //透支额度
    public function getCreditAttr($value, $data){
		return floatval($data['credit']);
    }

    //头像
    function getAvatarAttr($val, $data){
        $avatar = $data['avatar'] ? : 'https://wages-zymaimai.oss-cn-beijing.aliyuncs.com/2023/03/02/640060c849864.jpg';
        return $avatar;
    }

    //业务员姓名
    function getYewuNameAttr($val, $data){
        $text = '';
        if($data['yw_id']){
            $model = M('Admin');
            $wdata['aId'] = $data['yw_id'];
            $text = $model->where($wdata)->getField('aName');
        }
        return $text;
    }

    //客户属性
    function getAttrTextAttr($val, $data){
        $text = '';
        if($data['customer_attr']==1){
            $text = '充值客户';
        }else if($data['customer_attr']==2){
            $text = '月结客户';
        }else if($data['customer_attr']==3){
            $text = '现金客户';
        }else if($data['customer_attr']==4){
            $text = '欠款客户';
        }
        return $text;
    }

    //性别
    function getSexTextAttr($val, $data){
        $text = '';
        if($data['aSex']==0){
            $text = '女';
        }else if($data['aSex']==1){
            $text = '男';
        }else if($data['aSex']==2){
            $text = '未知';
        }
        return $text;
    }

    //类型
    function getTypeNameAttr($val, $data){
        $text = '';
        if($data['aSex']==0){
            $text = '女';
        }else if($data['aSex']==1){
            $text = '男';
        }else if($data['aSex']==2){
            $text = '未知';
        }
        return $text;
    }

    /**
     * 验证登录
     * @param int $form     //1就是默认，2:小程序 3:app
     * @return array
     */
    public function login($form=1, $_post){
        $aUser = $_post["aUser"];
        $wdata['username|customer_no|mobile'] = $aUser;
        $wdata['types'] =0;
        $user = $this->where($wdata)->find();
        if(!$user){
            $this->error = '客户账号不存在';
            return false;
        }
        $salt = $user['salt'];
        $aPwd = md5(md5($_post["aPwd"]) . $salt);
        if($aPwd!=$user['password']){
            $this->error = '密码错误！';
            return false;
        }
        if($user["issh"] == 0){
            $this->error = '对不起，该客户账户还在审核中！';
            return false;
        }
        $data_cc['login_time'] = time();
        if(empty($user['pass_word'])){
            $aNewPwd="666888";
            $model_user=M("user"); 
            $salt2= $this->GetRandStr(6);
            $data_cc["pass_word"] =$this->getEncryptPassword($aNewPwd,$salt2);
            $data_cc["salts"]=$salt2;
        }
        $this->get_baseinfo($user['uid']);
        $this->where($wdata)->save($data_cc);
		$ut_model = D('Token');
        $r = $ut_model->setToken($user, $form, 2);
        if(!$r['status']){
            $this->error = 'Token生成失败';
            return false;
        }
        $user['token'] = $r['data'];
        return $user;
    }

    public function loginc($form=1, $_post){
        $aUser = $_post["aUser"];
        $wdata['username|customer_no|mobile'] = $aUser;
        $wdata['types'] =0;
        $wdata['uid'] =$_post['uid'];
        $user = $this->where($wdata)->find();
        if(!$user){
            $this->error = '客户账号不存在';
            return false;
        }
        if($user["issh"] == 0){
            $this->error = '对不起，该客户账户还在审核中！';
            return false;
        }
        $data_cc['login_time'] = time();
        if(empty($user['pass_word'])){
            $aNewPwd="666888";
            $model_user=M("user"); 
            $salt2= $this->GetRandStr(6);
            $data_cc["pass_word"] =$this->getEncryptPassword($aNewPwd,$salt2);
            $data_cc["salts"]=$salt2;
        }
        $this->get_baseinfo($user['uid']);
        $this->where($wdata)->save($data_cc);
		$ut_model = D('Token');
        $r = $ut_model->setToken($user, $form, 2);
        if(!$r['status']){
            $this->error = 'Token生成失败';
            return false;
        }
        $user['token'] = $r['data'];
        return $user;
    }

    
 
    //二级密码验证 tokencc
    public function login_cc($form=1, $detail){
        $token=$detail['token'];
        $password=$detail['password'];
        $ut_token = M('token');
        $ut_user = M('user');
        $token_info=$ut_token->where(['token'=>$token])->find();
        $aId=$token_info['aId'];
        $user_info=$ut_user->where(['uid'=>$aId])->find();  
        if($user_info){
            $salt = $user_info['salts'];
            $aPwd=$this->getEncryptPassword($password,$salt);
            if($aPwd!=$user_info['pass_word']){
                $this->error = '密码验证错误！';
                return false;
            }
            if($user_info["issh"] == 0){
                $this->error = '对不起，该客户账户还在审核中！';
                return false;
            }
        }else{
            $this->error = '客户账号不存在';
            return false;
        }
		$ut_model = D('Token');
        $r = $ut_model->setTokencc($user_info, $form, 2);
        if(!$r['status']){
            $this->error = 'Token生成失败';
            return false;
        }
        $user_info['token_cc'] = $r['data'];
        return $user_info;
    }
    function get_baseinfo($uid){
        $user_config=M("user_config");
        $count= $user_config->where(['uid'=>$uid])->count();
        $data['uid']=$uid;
        $data['names']='门店管理系统';
        $data['logo']='https://wages-zymaimai.oss-cn-beijing.aliyuncs.com/2023/04/24/64464423a31fd.png';
        $data['bg_img']='https://wages-zymaimai.oss-cn-beijing.aliyuncs.com/2023/04/24/6446443020ebe.jpg';
        if($count==0){
           //系统设置默认logo和默认背景图默认门店名称
           $user_config->add($data);
        } 
    }
 
    //资金修改
    function changeAmount($input){
        //用户资金记录
        $f_wdata['uid'] = $input['uid'];
        if(!$input['uid']){
            $return['msg'] = '客户必须 - 1000';
            return $return;
        }
        if(!$input['amount'] || $input['amount']<0){
            $return['msg'] = '金额必须确为大于0 - 1001';
            return $return;
        }
        if(!$input['type']){
            $return['msg'] = '类型必须 - 1000';
            return $return;
        }
        $f_wdata['type'] = $input['type'];//1:收入 2:支出
        $f_wdata['f_type'] = 101;//充值
        $f_wdata['val'] = $input['amount'];
        if($input['pay_date'])$f_wdata['pay_time'] = strtotime($input['pay_date']);
        $f_wdata['remark'] = $input['remark'] ? : '';
        $f_wdata['aid'] = UID ? : 0;
        $f_wdata['source'] = 'User';
        $f_wdata['source_id'] = $input['uid'];
        $res = userMoneyUpdate($f_wdata);
        if(!$res['status']){
            $return['msg'] = $res['msg'].' - 1003';
            return $return;
        }
        //财务记录
        $f_wdata['uid'] = $input['uid'];
        $f_wdata['type'] = 1;//收入
        $f_wdata['f_type'] = 301;//订单付款
        if($input['pay_date'])$f_wdata['pay_time'] = strtotime($input['pay_date']);
        $f_wdata['val'] = $input['amount'];
        $f_wdata['remark'] = "用户充值[{$input['uid']}]";
        $f_wdata['aid'] =  UID ? : 0;
        $f_wdata['source'] = 'User';
        $f_wdata['source_id'] = $input['uid'];
        $res = fund($f_wdata);
        if(!$res['status']){
            $return['msg'] = $res['msg'].' - 1003';
            return $return;
        }
        $sqls = array_merge($sqls, $res['sqls']);

        return $res;
    }

    //格式化
    function listFormat($list){
        foreach($list as &$val){
            unset($val['password']);
        }
        return $list;
    }

    //搜索后置
    function _after_search(){
        $map = array();
        if(I('aName')){
            //模糊搜索
            $map['aName'] = array('like','%'.I('aName').'%');
        }
        if(I('keyword')){
            //模糊搜索
            $map['uid|names|mobile'] = array('like', '%' . $post['keyword'] . '%');
        }
        //dump($map);exit;
        return $map;
    }

    //用户基本信息(不包含密码)
    function detail($uid){
        $return = [
            'status' => false,
            'errcode'   => 10000,
            'data'   => '',
            'msg'    => '失败'
        ];
        $model = D('User');
        $user = $model->find($uid);
        if(!$user){
            return [];
        }
        unset($user['password']);
        return $user;
    }
    function GetRandStr($length){
        $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
        $randStr = str_shuffle($str);//打乱字符串
        $rands= substr($randStr,0,$length);//substr(string,start,length);返回字符串的一部分
        return $rands;
    }

    function getEncryptPassword($password, $salt = ''){
        return md5(md5($password) . $salt);
    }
   
}

?>